package com.dk.controller;

import com.dk.data.Result;
import com.dk.em.ConstantEnum;
import com.dk.entity.TAdminDTO;
import com.dk.feign.AdminFeign;
import com.dk.feign.CarSeekFeign;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/recommendController")
public class RecommendController implements Job {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CarSeekFeign tCarFeign;

    @Autowired
    private AdminFeign tAdminFeign;

    private final String NUM =  "_NUM";//次数
    private final String TIME = "_TIME";//时间
    private final String ZONG = "_NEW";//最后的key


    @ApiOperation(value = "智能推荐")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lname",value = "标签",required = true),
            @ApiImplicitParam(name = "id",value = "登录用户id",required = true)
    })
    @GetMapping("/show")
    public Result show(String lname, String id) {
        //截取时间戳
        Long t = new Date().getTime();
        String time1 = t.toString();
        time1 = time1.substring(5, 8);  //截取时间戳第5到8位
        t = Long.parseLong(time1);

        //遍历标签, 在循环里面进行标签判断
        String[] lname1 =  lname.split(",");
        for (String path : lname1) {
            //path : 每一个标签
            //以用户id + “TIME”+ 标签 为key
            //最近时间
            redisTemplate.boundZSetOps(id + TIME).incrementScore(path, Double.valueOf(t));
            //以用户id + “NUM”+ 标签 为key
            //有加1, 没有赋值1
            //标签数量
            redisTemplate.boundZSetOps(id + NUM).incrementScore(path, 1);
        }
        return Result.bulid(ConstantEnum.SUCCESS).setResult("成功");
    }

    @ApiOperation(value = "清洗标签并返回结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "登录用户id",paramType = "query")
    })
    @GetMapping("test")
    public Result test(@RequestParam String id){
        //获取该用户最近的三个标签
        Set setime = redisTemplate.boundZSetOps(id + TIME).reverseRange(0, 2);
        //清洗标签(迭代器遍历)
        Iterator iterator1 = setime.iterator();
        //用set集合接收,可以防止重复的标签,在页面显示
        Set<String> threeList2 = new HashSet<>();
        while (iterator1.hasNext()) {
            //转成String格式
            String str = (String) iterator1.next();
            //获取标签的次数
            Double score = redisTemplate.boundZSetOps(id + NUM).score(str);
            //大于三次的放进set集合里面  主要是为了防止意外点到的标签
            if (score > 3){
                threeList2.add(str);
            }
        }
        //如果标签个数少于3个,再去次数里找最多的三个
        if (threeList2.size() < 3){
            //次数最多的三个标签
            Set set = redisTemplate.boundZSetOps(id + NUM).reverseRange(0, 2);
            //迭代器遍历
            Iterator iterator = set.iterator();
            while (iterator.hasNext()){
                String s = (String) iterator.next();
                //放入我们的新集合里面
                threeList2.add(s);
            }
        }
        System.out.println(threeList2);
        //存放的集合
        ArrayList arrayList = new ArrayList();
        for (String path : threeList2) {
            //遍历集合,去除每一个标签,进行去搜索
            Result infoIdBy = tCarFeign.getInfoByTitle(path);
            arrayList.add(infoIdBy);
        }
        System.out.println(arrayList);
        return Result.bulid(ConstantEnum.SUCCESS).setResult(arrayList);
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext){
        Result<TAdminDTO> infoIdBy = tAdminFeign.getInfoIdBy();
        List<Integer> result = (List<Integer>) infoIdBy.getResult();
        for (Integer path : result) {
            test(path.toString());
        }
    }
}
